import { Inject, Controller, Body,  Post, Files, Fields } from '@midwayjs/decorator';
import { Context } from '@midwayjs/koa';
import { Validate } from '@midwayjs/validate';
import { ErrCode } from '../common/ErrCode';
import { GetSystemConfigDTO, SetSystemConfigDTO } from '../dto/admin.dto';
import { AddPartnerTypeDTO, AdminQueryPartnerListDTO, HandlePartnerGroupDTO, PartnerHadlerDTO, PartnerInfoDTO } from '../dto/partner.dto';
import { partnerGroupBuy } from '../entity/partnerGroupBuy.entity';
import { PartnerInfo } from '../entity/partnerInfo.entity';
import { UserInfo } from '../entity/user.entity';
import { IReturnData } from '../interfaces/IBase.interface';
import { PartnerService } from '../service/partner.service';
import { SystemConfigService } from '../service/system.service';
import { UserService } from '../service/user.service';

@Controller('/admin')
export class AdminController {
  @Inject()
  ctx: Context;

  @Inject()
  userService: UserService;

  @Inject()
  partnerService:PartnerService;

  @Inject()
  systemService:SystemConfigService;

  @Post('/upload')
  async Uploadfile(@Files() files,@Fields() fields):Promise<any>{
    
  }

  @Post('/setConfig')
  @Validate()
  async setConfig(@Body() data:SetSystemConfigDTO):Promise<IReturnData<any>>{
      let result:ErrCode =  await this.systemService.setSystemConfig(data.type,data.datas);
      return {code:result}
  }

  @Post('/getConfig')
  @Validate()
  async getConfig(@Body() data:GetSystemConfigDTO):Promise<IReturnData<any>>{
      let result:any = await this.systemService.getSystemConfig(data.type);
      if(result == null){
        return {code:ErrCode.SYSTEM_FAIL};
      }
      return {code:ErrCode.SYSTEM_SUCCESS,data:result};
  }
  @Post('/partnerlist')
  @Validate()
  async getPartnerList(@Body() data:AdminQueryPartnerListDTO):Promise<IReturnData<any>>{
      let list:PartnerInfo[] = await this.partnerService.getPartnerListByStatus({page:data.page,limit:data.limit},data.status);
      for(let i in list){
        list[i].detailsImage = JSON.parse(list[i].detailsImage);
        list[i].thumbnailImage = JSON.parse(list[i].thumbnailImage);
      }
      return {code:ErrCode.SYSTEM_SUCCESS,data:list};
  }

  @Post('/addPartner')
  @Validate()
  async AddPartnerInfo(@Body() data:PartnerInfoDTO):Promise<IReturnData<any>>{
      let code:ErrCode = await this.partnerService.addPartnerInfo(data);
      return {code:code};
  }

  @Post('/handlePartner')
  @Validate()
  async handlePartner(@Body() data:PartnerHadlerDTO):Promise<IReturnData<any>>{
        //检查 当前项目是否有未完成的订单（拼团）

        //对结果进行操作
      let code:ErrCode = await this.partnerService.updatePartnerStatus(data.id,data.status);
      return {code:code};
  }

  @Post('/addPartnerType')
  @Validate()
  async AddPartnerType(@Body() data:AddPartnerTypeDTO):Promise<IReturnData<any>>{
      let code:ErrCode = await this.partnerService.addPartnerType(data.name);
      return {code:code};
  }
  
  @Post('/getPartnerType')
  @Validate()
  async getPartnerType(@Body() data:any):Promise<IReturnData<any>>{
      let list = await this.partnerService.GetPartnerTypeList();
      return {code:ErrCode.SYSTEM_SUCCESS,data:list}
  }

  @Post('/handleGroup')
  @Validate()
  async handlePartnerGroup(@Body() data:HandlePartnerGroupDTO):Promise<IReturnData<any>>{
    let result:ErrCode = await this.partnerService.HandleGroupStatus(data.groupid,data.handleStatus);  
    return {code:result};
  }

  @Post('/partnerGroupList')
  @Validate()
  async partnerGroupList(@Body() data:AdminQueryPartnerListDTO):Promise<IReturnData<any>>{
      let list:partnerGroupBuy[] = await this.partnerService.getGroupByStatus({page:data.page,limit:data.limit},data.status);
      let userids:number[] = [];
      let partnerids:number[] = [];
      for(let i in list){
        let ulist = list[i].userids.split(',').map(Number);
        userids =  userids.concat(ulist);
        partnerids.push(list[i].partnerid);
      }
      let userlist:UserInfo[] = await this.userService.getUserinfoById(userids.filter((item,index,userids)=>{
        return userids.indexOf(item,0) == index;
      }));
      userlist.map((item)=>{
        for(let i in list){
            let ulist = list[i].userids.split(',').map(Number);
            if(ulist.indexOf(item.userid) != -1){
                let userlist = list[i]['userlist'];
                if(userlist == null){
                  list[i]['userlist'] = [];
                  userlist = list[i]['userlist'];
                }
                userlist.push({
                    name:item.username,
                    phone:item.phone,
                    headimg:item.headimg
                });
                // break;
            }
          }
      });
      let partnerInfos:PartnerInfo[] = await this.partnerService.getPartnerByIds(partnerids);
      partnerInfos.map((item)=>{
        for(let i in list){
          if(item.id == list[i].partnerid){
            list[i]['partnerInfo'] = item;
            // break;
          }
        }
      });

      return {code:ErrCode.SYSTEM_SUCCESS,data:list}
  }
}
